
#ifndef __USB_H__
#define __USB_H__

#include "stdtype.h"

//USB Registers define
#define	USB_Base			0x0000

#define	USB_in0buf		(USB_Base+0x700)														//0x700~0x707
#define	USB_in1buf		(USB_Base+0x680)														//0x680~0x687
#define USB_in2buf		(USB_Base+0x600)														//0x600~0x607
#define USB_out0buf		(USB_Base+0x6c0)														//0x6c0~0x6c7
#define	USB_ivec		(*(volatile U8 xdata*)(USB_Base+0x7a8))
#define	USB_in07irq		(*(volatile U8 xdata*)(USB_Base+0x7a9))
#define	USB_out07irq	(*(volatile U8 xdata*)(USB_Base+0x7aa))
#define	USB_usbirq		(*(volatile U8 xdata*)(USB_Base+0x7ab))
#define	USB_in07ien		(*(volatile U8 xdata*)(USB_Base+0x7ac))
#define	USB_out07ien	(*(volatile U8 xdata*)(USB_Base+0x7ad))
#define	USB_usbien		(*(volatile U8 xdata*)(USB_Base+0x7ae))
#define	USB_usbbav		(*(volatile U8 xdata*)(USB_Base+0x7af))
#define	USB_ep0cs		(*(volatile U8 xdata*)(USB_Base+0x7b4))
#define	USB_in0bc		(*(volatile U8 xdata*)(USB_Base+0x7b5))
#define	USB_in1bc		(*(volatile U8 xdata*)(USB_Base+0x7b7))
#define	USB_in2bc		(*(volatile U8 xdata*)(USB_Base+0x7b9))
#define	USB_in1cs		(*(volatile U8 xdata*)(USB_Base+0x7b6))
#define	USB_in2cs		(*(volatile U8 xdata*)(USB_Base+0x7b8))
#define	USB_out0bc		(*(volatile U8 xdata*)(USB_Base+0x7c5))
#define	USB_out1bc		(*(volatile U8 xdata*)(USB_Base+0x7c7))
#define	USB_out2bc		(*(volatile U8 xdata*)(USB_Base+0x7c9))
#define	USB_out1cs		(*(volatile U8 xdata*)(USB_Base+0x7c6))
#define	USB_out2cs		(*(volatile U8 xdata*)(USB_Base+0x7c8))
#define	USB_usbcs		(*(volatile U8 xdata*)(USB_Base+0x7d6))
#define	USB_togctl		(*(volatile U8 xdata*)(USB_Base+0x7d7))
#define	USB_usbframel	(*(volatile U8 xdata*)(USB_Base+0x7d8))
#define	USB_usbframeh	(*(volatile U8 xdata*)(USB_Base+0x7d9))
#define	USB_fnaddr		(*(volatile U8 xdata*)(USB_Base+0x7db))
#define	USB_usbpair		(*(volatile U8 xdata*)(USB_Base+0x7dd))
#define	USB_in07val		(*(volatile U8 xdata*)(USB_Base+0x7de))
#define	USB_out07val	(*(volatile U8 xdata*)(USB_Base+0x7df))
#define	USB_setupbuf	(USB_Base+0x7e8)														//0x7e8~0x7ef

//USB Macros
#define USB_SendZeroLengthPacket			USB_ep0cs = 0x02							//Clear HSNAK bit
#define USB_Stall							USB_ep0cs = 0x01							//set EP0 stall
#define USB_ACK								USB_ep0cs = 0x02							//Clear HSNAK bit

//USB interrupt vectors
#define	USB_SETUP_DATA_VALID_INT			0x00
#define	USB_START_OF_FRAME_INT				0x04
#define	USB_SETUP_TOKEN_INT					0x08
#define	USB_SUSPEND_INT						0x0c
#define	USB_RESET_INT						0x10
#define	USB_EP0_IN_INT						0x18
#define	USB_EP0_OUT_INT						0x1c
#define	USB_EP1_IN_INT						0x20
#define	USB_EP2_IN_INT						0x28

//USB Endpoint buffer length define
#define	USB_EP0_LENGTH						8
#define	USB_EP1_LENGTH						8
#define	USB_EP2_LENGTH						8

//---------------------------------------------------------------------
//Define USB Parameters
//---------------------------------------------------------------------

#define BCDUSB                                    0x0110                // USB 1.1
#define BCDHID                                    0x0111                // HID 1.11
#define VID                                       0x3528			//0x2571                // Vendor ID
#define PID                                       0x1255			//0x4101                // Product ID for ET81P304
//#define VID                                       0x0416                // Vendor ID
//#define PID                                       0x1010                // Product ID for ET81P304
#define DEV_RELEASE_NO                            0x0100                // Product version
#define INTERFACE_NO		                      2                     // Number of Interface (2 interface for keyboard and mouse)

#define VENDOR_PID									0x0478
#define VENDOR_INTERFACE_NO		                    1


//Request Type
#define STANDARD								  0x00
#define CLASS                                     0x20
#define VENDOR                                    0x40

//Request Recipient
#define DEVICE										0x00
#define INTERFACE									0x01
#define ENDPOINT									0x02

//Standard Feature Selectors
#define DEVICE_REMOTE_WAKEUP						0x01
#define ENDPOINT_HALT								0x00

//Standard Descriptor Types
#define USB_DEVICE_DESCRIPTOR_TYPE                	0x01
#define USB_CONFIGURATION_DESCRIPTOR_TYPE         	0x02
#define USB_STRING_DESCRIPTOR_TYPE                	0x03

#define USB_INTERFACE_DESCRIPTOR_TYPE             	0x04
#define USB_ENDPOINT_DESCRIPTOR_TYPE              	0x05
#define USB_DEIVCE_QUALIFER_DESCRIPTOR_TYPE       	0x06
#define USB_OTHERSPEED_DESCRIPTOR_TYPE            	0x07

// CLASS DESCRIPTOR TYPES
#define USB_HID_DESCRIPTOR_TYPE                   	0x21
#define USB_REPORT_DESCRIPTOR_TYPE                	0x22
#define USB_PHYSICAL_DESCRIPTOR_TYPE              	0x23

// Standard Request
#define	USB_REQUEST_GET_STATUS						0x00
#define	USB_REQUEST_CLEAR_FEATURE              		0x01
#define	USB_REQUEST_SET_FEATURE                		0x03
#define	USB_REQUEST_SET_ADDRESS                		0x05
#define	USB_REQUEST_GET_DESCRIPTOR             		0x06
#define	USB_REQUEST_SET_DESCRIPTOR             		0x07
#define	USB_REQUEST_GET_CONFIGURATION          		0x08
#define	USB_REQUEST_SET_CONFIGURATION          		0x09
#define	USB_REQUEST_GET_INTERFACE              		0x0A
#define	USB_REQUEST_SET_INTERFACE              		0x0B
#define	USB_REQUEST_SYNCH_FRAME                		0x0C

// HID class-specific Requests
#define	USB_REQUEST_GET_REPORT						0x01
#define	USB_REQUEST_GET_IDLE						0x02
#define	USB_REQUEST_GET_PROTOCOL					0x03
#define	USB_REQUEST_SET_REPORT						0x09
#define	USB_REQUEST_SET_IDLE						0x0A
#define	USB_REQUEST_SET_PROTOCOL					0x0B

// Standard Descriptors
#define	USB_DEVICE_DESCRIPTOR                  0x01
#define	USB_CONFIGURATION_DESCRIPTOR           0x02
#define	USB_STRING_DESCRIPTOR                  0x03
#define	USB_INTERFACE_DESCRIPTOR               0x04
#define	USB_ENDPOINT_DESCRIPTOR                0x05

// HID Class-Specific Descriptors
#define	USB_HID_DESCRIPTOR                     0x21
#define	USB_REPORT_DESCRIPTOR                  0x22
#define	USB_PHYSICAL_DESCRIPTOR                0x23

// Standard Descriptor data length
#define	CONFIG_DESCRIPTOR_SIZE                	9
#define	INTERFACE_DESCRIPTOR_SIZE             	9
#define	HID_DESCRIPTOR_SIZE                   	9
#define	ENDPOINT_DESCRIPTOR_SIZE              	7
#define	CONFIGURATION_TOTAL_SIZE	            59
#define	VENDOR_CONFIGURATION_TOTAL_SIZE        	32

// PID Types
#define PID_OUT																	0x10
#define PID_IN																	0x90
#define PID_SOF																	0x50
#define PID_SETUP																0xD0
#define PID_DATA0																0x30
#define PID_DATA1																0xB0
#define PID_ACK																	0x20
#define PID_NAK																	0xA0
#define PID_STALL																0xE0
#define PID_PRE																	0xC0

typedef struct   //Standard setup command
{                                        
    U8 Type;                          
    U8 Request;                       
    U8 ValueL;                        
    U8 ValueH;                        
    U8 IndexL;                        
    U8 IndexH;                        
    U8 LnL;                           
    U8 LnH;                           
}SETUP_PACKET; 

void USB_Test(void);
void USB_Reset(void);
bit USB_Suspend(void);
void USB_Wakeup(void);
void USB_Initial(void);
void USB_interrupt(void);
void USB_EP0_SetupCommand(void);
void USB_SetFeature(void);
void USB_ClearFeature(void);
void USB_GetStatus(void);
void USB_GetDescriptor(void);
void USB_GetConfiguration(void);
void USB_GetInterface(void);
void USB_SetReport(void);
void USB_SetProtocol(void);
void USB_GetProtocol(void);
void USB_SetIdle(void);
void USB_GetIdle(void);
void USB_EndPoint0_DataIn(U8 /*code*/ *SrcPtr, U16 TransferLength);
U8 USB_Endpoint0_DataOut(void);
void USB_EndPoint1_DataIn(void);
void USB_EndPoint2_DataIn(void);
void USB_Get_HidData(void);
void USB_Down_Safety(void);



#endif